package com.wen.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wen.edu.entity.EduChapter;
import com.wen.edu.entity.EduVideo;
import com.wen.edu.entity.vo.ChapterVO;
import com.wen.edu.entity.vo.VideoVO;
import com.wen.edu.mapper.EduChapterMapper;
import com.wen.edu.service.EduChapterService;
import com.wen.edu.service.EduVideoService;
import com.wen.servicebase.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author wen
 * @since 2021-05-29
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Resource
    private EduVideoService eduVideoService;

    /**
     * 根据相关的课程ID来获取对应的课程大纲（课程章节和小节）
     *
     * @param courseId 课程ID
     * @return result of operation
     */
    @Override
    public List<ChapterVO> getChapterVideoByCourseId(String courseId) {
//        先查询符合条件的课程章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(wrapperChapter);
//        新建一个空的ChapterVO集合来存储符合条件的结果
        List<ChapterVO> chapterVOList = new ArrayList<>();
        for (EduChapter eduChapter : eduChapters) {
            ChapterVO chapterVO = new ChapterVO();
//            复制eduChapter属性到chapterVO
            BeanUtils.copyProperties(eduChapter, chapterVO);
//            查询每个章节下对应的小节
            QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
            wrapperVideo.eq("course_id", courseId);
            wrapperVideo.eq("chapter_id", eduChapter.getId());
            List<EduVideo> eduVideos = eduVideoService.list(wrapperVideo);
//           新建一个空的VideoVO集合来存储符合条件的结果
            List<VideoVO> videoVOList = new ArrayList<>();
            for (EduVideo eduVideo : eduVideos) {
                VideoVO videoVO = new VideoVO();
//            复制eduVideo属性到videoVO
                BeanUtils.copyProperties(eduVideo, videoVO);
                videoVOList.add(videoVO);
            }
            chapterVO.setChildren(videoVOList);
//            添加chapterVO对象到集合
            chapterVOList.add(chapterVO);
        }
        return chapterVOList;
    }

    /**
     * 根据章节id删除章节信息
     *
     * @param chapterId 章节ID
     */
    @Override
    public void deleteChapter(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = eduVideoService.count(wrapper);
        if (count > 0)
            throw new BusinessException("请清空对应的小节数据后，再删除章节~~~");
        else {
            int delete = baseMapper.deleteById(chapterId);
            if (delete == 0)
                throw new BusinessException("章节删除失败~~~");
        }
    }

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }
}
